<?php
//测试控制器类
class IndexController extends Controller{

    private $db;
    private $user;

    public function __init(){
        $user = Q('session.user','');
        //指定函数
        $this -> db = k("Index");
        $this -> user = $user;
        $this -> assign('user',$user);
    }
    //动作方法
    public function index(){
        $cid = Q('get.cid','0');
        $op = Q('get.op','');
        $key = Q('get.key','');
        $user = $this -> user;
        //判断用户VIP等级
        if(empty($user)){
            $vip = 0;
        }else{
            $vip = $user['uvip'];
        }
        //获取商品类别
        $catelist = k('Cate') -> catelist();

        //获取商品列表
        if(empty($cid)){
            $where = '';
        }else{
            $where = 'goods.cid ='.$cid;
        }

        if(!empty($key)){
            $where = 'CONCAT(gname) like "%'.$key.'%"';
        }

        //排序
        if(!empty($op)){
            if(empty($where)){
                $where = '1';
            }
            if($op == 'time'){
                $where .= " ORDER BY addtime DESC";
            }
            if($op == 'money'){
                if($vip==0){
                    $where .= " ORDER BY reprice ASC";
                }
                if($vip==1){
                    $where .= " ORDER BY v1price ASC";
                }
                if($vip==2){
                    $where .= " ORDER BY v2price ASC";
                }
            }
        }

        //获取翻页参数
        $ordernum = k('Goods') -> selc_num($where);
        //翻页参数
        $page = new page($ordernum,10);
        $limit = $page->limit();

        //获取商品
        $goodlist = k('Goods') -> glist($where,$limit);

        $this -> assign('catelist',$catelist);
        $this -> assign('cid',$cid);
        $this -> assign('goodslist',$goodlist);
        $this -> assign('vip',$vip);
        $this -> assign('op',$op);
        $this -> assign('page',$page->show(0));
        //显示视图
        $this->display('Template/index/header');
        $this->display('left');
        $this->display();
    }
}
